<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form :model="listParm"
             ref="form"
             label-width="80px"
             :inline="true"
             size="small">
      <el-form-item>
        <el-input placeholder="请输入姓名"
                  v-model="listParm.nickName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input placeholder="请输入电话"
                  v-model="listParm.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search"
                   @click="searchBtn">搜索</el-button>
        <el-button icon="el-icon-close"
                   style="color: #ff7670"
                   @click="resetBtn">重置</el-button>
        <el-button type="primary"
                   icon="el-icon-plus"
                   @click="addBtn"
                   v-permission="['sys:user:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格  -->
    <el-table :height="tableHeight"
              :data="tableData"
              border
              stripe>
      <el-table-column prop="nickName"
                       label="姓名"></el-table-column>
      <el-table-column prop="phone"
                       label="电话"></el-table-column>
      <el-table-column prop="email"
                       label="邮箱"></el-table-column>
      <el-table-column label="操作"
                       width="300"
                       align="center">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-edit"
                     @click="editBtn(scope.row)"
                     v-permission="['sys:user:edit']">编辑</el-button>
          <el-button type="danger"
                     size="small"
                     icon="el-icon-edit"
                     @click="resetPswBtn(scope.row)"
                     v-permission="['sys:user:delete']">重置密码</el-button>
                     <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     @click="deleteBtn(scope.row)"
                     v-permission="['sys:user:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination @size-change="sizeChange"
                   @current-change="currentChange"
                   :current-page.sync="listParm.currentPage"
                   :page-sizes="[10, 20, 40, 80, 100]"
                   :page-size="listParm.pageSize"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="listParm.total"
                   background>
    </el-pagination>
    <!-- 新增或编辑弹框组件 -->
    <sys-dialog :title="dialog.title"
                :visible="dialog.visible"
                :width="dialog.width"
                :height="dialog.height"
                @onClose="onClose"
                @onConfirm="onConfirm">
      <div slot="content">
        <el-form :model="addModel"
                 ref="addRef"
                 :rules="rules"
                 label-width="80px"
                 size="small"
                 style="margin-right: 40px">
          <el-row>
            <el-col :span="12"
                    :offset="0">
              <el-form-item prop="nickName"
                            label="姓名">
                <el-input v-model="addModel.nickName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12"
                    :offset="0">
              <el-form-item prop="phone"
                            label="电话">
                <el-input v-model="addModel.phone"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12"
                    :offset="0">
              <el-form-item prop="email"
                            label="邮箱">
                <el-input v-model="addModel.email"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12"
                    :offset="0">
              <el-form-item prop="sex"
                            label="性别">
                <el-radio-group v-model="addModel.sex">
                  <el-radio :label="'0'">男</el-radio>
                  <el-radio :label="'1'">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12"
                    :offset="0">
              <el-form-item prop="username"
                            label="账户">
                <el-input v-model="addModel.username"></el-input>
              </el-form-item>
            </el-col>
            <el-col v-if="addModel.type == '0'"
                    :span="12"
                    :offset="0">
              <el-form-item prop="password"
                            label="密码">
                <el-input v-model="addModel.password"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12"
                    :offset="0">
              <el-form-item prop="username"
                            label="角色">
                <el-select v-model="addModel.roleId"
                           placeholder="请选择">
                  <el-option v-for="item in options"
                             :key="item.roleId"
                             :label="item.roleName"
                             :value="item.roleId">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </sys-dialog>
  </el-main>
</template>

<script>
import {
  getListApi,
  addUserApi,
  editUserApi,
  deleteUserApi,
  getRoleListApi,
  getRoleIdApi,
  resetPasswordApi,
} from '@/api/user'
//引入弹框组件
import SysDialog from '@/components/dialog/SysDialog.vue'
export default {
  //注册弹框组件
  components: {
    SysDialog,
  },
  data() {
    return {
      options: [],
      //表单验证规则
      rules: {
        nickName: [
          {
            trigger: 'blur',
            required: true,
            message: '请填写姓名',
          },
        ],
        phone: [
          {
            trigger: 'blur',
            required: true,
            message: '请填写电话',
          },
        ],
        sex: [
          {
            trigger: 'blur',
            required: true,
            message: '请选择性别',
          },
        ],
        username: [
          {
            trigger: 'blur',
            required: true,
            message: '请填写账户',
          },
        ],
        password: [
          {
            trigger: 'blur',
            required: true,
            message: '请填写密码',
          },
        ],
        email: [
          {
            trigger: 'blur',
            required: true,
            message: '请填写邮箱',
          },
        ],
      },
      //表单绑定的数据
      addModel: {
        roleId: '',
        type: '',
        userId: '',
        nickName: '',
        phone: '',
        email: '',
        sex: '',
        username: '',
        password: '',
      },
      //弹框属性
      dialog: {
        title: '',
        visible: false,
        width: 630,
        height: 230,
      },
      //表格的高度
      tableHeight: 0,
      listParm: {
        total: 0,
        pageSize: 10, //默认每页显示10条数据
        currentPage: 1,
        nickName: '',
        phone: '',
      },
      tableData: [],
    }
  },
  created() {
    this.getList()
    this.getRoleList()
  },
  methods: {
     async resetPswBtn(row) {
      let confirm = await this.$myconfirm('重置后密码为【666666】,确定重置密码吗?')
      if (confirm) {
        console.log(row)
        let res = await resetPasswordApi({
          userId: row.userId,
        })
        if (res && res.code == 200) {
          this.$message.success(res.msg)
          //刷新表格
          this.getList()
        }
      }
    },
    async getRoleId(userId) {
      let res = await getRoleIdApi({ userId: userId })
      console.log(res)
      if (res && res.code == 200) {
        this.addModel.roleId = res.data.roleId
      }
    },
    //重置按钮
    resetBtn() {
      this.listParm.nickName = ''
      this.listParm.phone = ''
      this.getList()
    },
    //搜索按钮
    searchBtn() {
      this.getList()
    },
    async getList() {
      let res = await getListApi(this.listParm)
      console.log('请求成功')
      console.log(res)
      if (res && res.code == 200) {
        this.tableData = res.data.records
        this.listParm.total = res.data.total
      }
    },
    //弹框的确定
    onConfirm() {
      //表单验证
      this.$refs.addRef.validate(async (valid) => {
        if (valid) {
          this.dialog.visible = false
          let res = null
          if (this.addModel.type == '0') {
            res = await addUserApi(this.addModel)
          } else {
            res = await editUserApi(this.addModel)
          }
          if (res && res.code == 200) {
            //刷新表单
            this.getList()
            this.$message({ type: 'success', message: res.msg })
          }
        }
      })
    },
    //弹框的取消
    onClose() {
      this.dialog.visible = false
    },
    //页数改变时触发
    currentChange(val) {
      this.listParm.currentChange = val
      this.getList()
    },
    //页容量改变时触发
    sizeChange(val) {
      this.listParm.pageSize = val
      this.getList()
    },
    //删除按钮
    async deleteBtn(row) {
      let confirm = await this.$myconfirm('确定删该数据吗？')
      if (confirm) {
        let res = await deleteUserApi({ userId: row.userId })
        if (res && res.code == 200) {
          this.$message({ type: 'success', message: res.msg })
          this.getList()
        }
      }
    },
    //编辑按钮
    editBtn(row) {
      //设置弹框属性
      this.dialog.title = '编辑用户'
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      //把当前要编辑的数据复制到表单数据域
      this.$objCopy(row, this.addModel)
      //设置编辑标识
      this.addModel.type = '1'
      this.getRoleId(row.userId)
    },
    async getRoleList() {
      let res = await getRoleListApi()
      console.log(res)
      this.options = res.data
    },
    //新增按钮
    addBtn() {
      //设置弹框属性
      this.dialog.title = '新增用户'
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      //设置编辑标识
      this.addModel.type = '0'
    },
  },
  mounted() {
    this.$nextTick(() => {
      //计算表格的高度
      this.tableHeight = window.innerHeight - 220
    })
  },
}
</script>

<style lang="scss" scoped>
</style>